<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <!--优惠券列表-->
    <el-table v-loading="loading" :data="couponList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" prop="couponId"/>
      <el-table-column label="优惠卷名称" align="center" prop="couponName"/>
      <el-table-column label="优惠卷类型" align="center" prop="couponType">
        <template slot-scope="scope">
          <span v-if="scope.row.couponType == 0">全场赠券</span>
          <span v-if="scope.row.couponType == 1">会员赠券</span>
          <span v-if="scope.row.couponType == 2">购物赠券</span>
          <span v-if="scope.row.couponType == 3">注册赠券</span>
        </template>
      </el-table-column>
      <el-table-column label="使用类型" align="center" prop="useType">
        <template slot-scope="scope">
          <span v-if="scope.row.useType == 0">全场通用</span>
          <span v-if="scope.row.useType == 1">指定分类</span>
          <span v-if="scope.row.useType == 2">指定商品</span>
        </template>
      </el-table-column>
      <el-table-column label="使用门槛" align="center" prop="minPoint">
        <template slot-scope="scope">
          <span v-if="scope.row.minPoint == 0">无门槛</span>
          <span v-if="scope.row.minPoint > 0">满{{ scope.row.minPoint }}元可用</span>
        </template>
      </el-table-column>
      <el-table-column label="面值" align="center" prop="couponPrice"/>
      <el-table-column label="使用平台" align="center" prop="platform">
        <template slot-scope="scope">
          <span v-if="scope.row.platform == 0">全部</span>
          <span v-if="scope.row.platform == 1">移动</span>
          <span v-if="scope.row.platform == 2">PC</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="endTime">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }}至{{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="endTime">
        <template slot-scope="scope">
          <span v-if="new Date(scope.row.endTime).getTime() > new Date().getTime()">未过期</span>
          <span v-else>未过期</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:post:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:post:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[3, 5, 7, 10]"
      @size-change="handleSizeChange"
      :page.sync="couponListRequest.pageNum"
      :limit.sync="couponListRequest.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="coupon" :rules="rules" label-width="80px">
        <el-form-item label="优惠卷类型" prop="couponType">
          <el-select v-model="coupon.couponType" clearable  placeholder="请选择优惠卷类型">
            <el-option label="全场赠券" :value="0"></el-option>
            <el-option label="会员赠券" :value="1"></el-option>
            <el-option label="购物赠券" :value="2"></el-option>
            <el-option label="注册赠券" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠卷名称" prop="couponName">
          <el-input v-model="coupon.couponName" placeholder="请输入优惠卷名称"/>
        </el-form-item>
        <el-form-item label="使用平台" prop="platform">
          <el-select v-model="coupon.platform" placeholder="请选择使用平台">
            <el-option label="全部" :value="0"></el-option>
            <el-option label="移动" :value="1"></el-option>
            <el-option label="PC" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总发行量" prop="couponCount">
          <el-input v-model="coupon.couponCount" placeholder="请输入总发行量"/>
        </el-form-item>
        <el-form-item label="面额" prop="couponPrice">
          <el-input placeholder="请输入面额" v-model="coupon.couponPrice">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="每人限领" prop="perLimit">
          <el-input v-model="coupon.perLimit" placeholder="请输入每人限领"/>
        </el-form-item>
        <el-form-item label="使用门槛" prop="minPoint">
          <el-input placeholder="请输入使用门槛" v-model="coupon.minPoint">
            <template slot="prepend">满</template>
            <template slot="append">元可用</template>
          </el-input>
        </el-form-item>
        <el-form-item label="领取日期" prop="enableTime">
          <el-date-picker
            v-model="coupon.enableTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择领取日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期" required>
          <el-col :span="11">
            <el-form-item prop="date1">
              <el-date-picker type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"
                              v-model="coupon.startTime" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">至</el-col>
          <el-col :span="11">
            <el-form-item prop="date2">
              <el-date-picker type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择时间"
                              v-model="coupon.endTime" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="可使用商品" prop="useType">
          <el-radio-group v-model="coupon.useType">
            <el-radio-button :label="0">全场通用</el-radio-button>
            <el-radio-button :label="1">指定分类</el-radio-button>
            <el-radio-button :label="2">指定商品</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="couponNote">
          <el-input v-model="coupon.couponNote"  type="textarea" :rows="2" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import ImageUpload from "../../../components/ImageUpload/index.vue";
import {
  addCoupon, deleteCoupon,
  updateCoupon,
} from "../../../api/marketing/coupon/coupon";
import {findCouponList} from "../../../api/marketing/coupon/coupon";

export default {
  name: "coupon",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
      //上传图片
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券表格数据
      couponList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      couponListRequest: {
        couponType: null,
        pageNum: 1,
        pageSize: 5
      },
      // 表单参数
      coupon: {},
      // 表单校验
      rules: {
        couponName: [
          {required: true, message: "请输入优惠卷名称", trigger: "blur"}
        ],
        couponCount: [
          {required: true, message: "请输入总发行量", trigger: "blur"}
        ],
        couponPrice: [
          {required: true, message: "请输入面额", trigger: "blur"}
        ],
        minPoint: [
          {required: true, message: "请输入使用门槛", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  comments: {
    imageUpload: ImageUpload
  },
  methods: {
    updateRecommendStatus(row) {
      console.log(row);
      if (row.couponId != undefined) {
        updateCoupon(row).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        });
      }
    }, handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.couponListRequest.pageSize = val;
      console.log(this.couponListRequest);
      this.getList()
    },
    /** 查询营销专题列表 */
    getList() {
      this.loading = true;
      findCouponList(this.couponListRequest).then(response => {
        this.couponList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.coupon = {
        //优惠券编号
        couponId: 0,
        //优惠卷类型；0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券
        couponType: undefined,
        //名称
        couponName: '',
        //使用平台：0->全部；1->移动；2->PC
        platform: undefined,
        //数量
        couponCount: 0,
        //金额
        couponPrice: 0,
        //每人限领张数
        perLimit: 0,
        //使用门槛；0表示无门槛
        minPoint: 0,
        //开始使用时间
        startTime: null,
        //结束使用时间
        endTime: null,
        //使用类型：0->全场通用；1->指定分类；2->指定商品
        useType: 0,
        //备注
        couponNote: '',
        //发行数量
        publishCount: 0,
        //已使用数量
        useCount: 0,
        //领取数量
        receiveCount: 0,
        //可以领取的日期
        enableTime: null,
        //优惠码
        couponCode: new Date().getTime()+'',
        //可领取的会员类型：0->无限制 1:黄金会员 2:铂金会员 3:钻石会员
        memberLevel: 0,
        //逻辑删除 0:未删除 1:已删除
        isDelete: 0,
        //优惠券状态：0->未过期；1->已过期
        couponStatus: 0
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.subjectId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.coupon = row;
      console.log(this.coupon);
      this.open = true;
      this.title = "修改优惠券";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.coupon.couponId != 0) {
            updateCoupon(this.coupon).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCoupon(this.coupon).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.couponId || this.ids;
      this.$modal.confirm('是否确认删除优惠券编号为"' + postIds + '"的数据项？').then(function () {
        return deleteCoupon(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
